package io.renren.modules.cards.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import io.renren.common.utils.DictionariesTypeKeys;
import io.renren.modules.cards.entity.AmgCdkAgentEntity;
import io.renren.modules.cards.entity.AmgCdkCardsEntity;
import io.renren.modules.cards.service.AmgCdkAgentService;
import io.renren.modules.cards.service.AmgCdkCardsService;
import io.renren.modules.sys.entity.SysDictionariesEntity;
import io.renren.modules.sys.service.SysDictionariesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-09-03 15:06:23
 */
@RestController
@RequestMapping("cards/amgcdkcards")
public class AmgCdkCardsController {
    @Autowired
    private AmgCdkCardsService amgCdkCardsService;
    @Autowired
    private SysDictionariesService sysDictionariesService;
    @Autowired
    private AmgCdkAgentService amgCdkAgentService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("cards:amgcdkcards:list")
    public R list(@RequestParam Map<String, Object> params){
        List<SysDictionariesEntity> cdkTypeList = sysDictionariesService.queryListByType(DictionariesTypeKeys.CDK_TYPE);
        List<SysDictionariesEntity> dateTypeList = sysDictionariesService.queryListByType(DictionariesTypeKeys.DATE_TYPE);
        List<AmgCdkAgentEntity> agentEntityList=amgCdkAgentService.queryForStatusList("");
        Map<String, String> cdkTypeMap = cdkTypeList.stream().collect(
                Collectors.toMap(SysDictionariesEntity::getCode, SysDictionariesEntity::getValue));
        Map<Integer, String> agentMap = agentEntityList.stream().collect(
                Collectors.toMap(AmgCdkAgentEntity::getId, AmgCdkAgentEntity::getName));
        Map<String, String> dateTypeMap = dateTypeList.stream().collect(
                Collectors.toMap(SysDictionariesEntity::getCode, SysDictionariesEntity::getValue));
        PageUtils page = amgCdkCardsService.queryPage(params);
        List<AmgCdkCardsEntity> list = (List<AmgCdkCardsEntity>) page.getList();
        for (AmgCdkCardsEntity entity : list) {
            entity.setCdkTypeName(cdkTypeMap.get(entity.getCdkType()));
            entity.setCdkDateTypeName(dateTypeMap.get(entity.getCdkType()));
            entity.setAgentName(agentMap.get(entity.getAgentId()));
        }
        return R.ok().put("page", page);
    }

    @RequestMapping("/batchAdd")
    @RequiresPermissions("cards:amgcdkcards:save")
    public R batchAdd(@RequestParam Map<String, Object> params){
        amgCdkCardsService.batchAdd(params);
        return R.ok();
    }

    /**
     * 获得下拉选项
     */
    @RequestMapping("/getDictonariesSelectList")
    @RequiresPermissions("cards:amgcdkcards:list")
    public R getDictonariesSelectList() {
        List<SysDictionariesEntity> cdkTypeList = sysDictionariesService.queryListByType(DictionariesTypeKeys.CDK_TYPE);
        List<SysDictionariesEntity> dateTypeList = sysDictionariesService.queryListByType(DictionariesTypeKeys.DATE_TYPE);
        List<AmgCdkAgentEntity> agentEntityList=amgCdkAgentService.queryForStatusList("0");
        R result = R.ok();
        result.put("cdkTypeList", cdkTypeList);
        result.put("dateTypeList", dateTypeList);
        result.put("agentList",agentEntityList);
        return result;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("cards:amgcdkcards:info")
    public R info(@PathVariable("id") Integer id){
        AmgCdkCardsEntity amgCdkCards = amgCdkCardsService.selectById(id);
        return R.ok().put("amgCdkCards", amgCdkCards);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("cards:amgcdkcards:save")
    public R save(@RequestBody AmgCdkCardsEntity amgCdkCards){
			amgCdkCardsService.insert(amgCdkCards);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("cards:amgcdkcards:update")
    public R update(@RequestBody AmgCdkCardsEntity amgCdkCards){
			amgCdkCardsService.updateById(amgCdkCards);
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/updateWholesale")
    @RequiresPermissions("cards:amgcdkcards:update")
    public R updateWholesale(@RequestParam Map<String, Object> params ){
        Integer[] ids= (Integer[]) params.get("ids");
        for (int i = 0; i < ids.length; i++) {

        }
//        amgCdkCardsService.updateBatchById()

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("cards:amgcdkcards:delete")
    public R delete(@RequestBody Integer[] ids){
			amgCdkCardsService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
